package com.dailyblue.project.estate.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.commons.bean.ChargeItem;
import com.dailyblue.project.estate.commons.handler.ChargeItemExcception;
import com.dailyblue.project.estate.commons.mapper.ChargeItemMapper;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import com.dailyblue.project.estate.service.ChargeItemService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:Liu
 * @Testname:ChargeItemServiceImpl
 * @Date:2025/9/18 14:09
 */
@Service
public class ChargeItemServiceImpl extends ServiceImpl<ChargeItemMapper, ChargeItem> implements ChargeItemService {
    @Resource
    private ChargeItemMapper chargeItemMapper;

    @Override
    public JsonResult saveChargeItem(ChargeItem chargeItem) {
        boolean b = save(chargeItem);
        if (!b) {
            throw new ChargeItemExcception("添加失败");
        }
        return ResultTool.success("save success");
    }

    @Override
    public JsonResult findOne(String id) {
        ChargeItem chargeItem = getById(id);
        return ResultTool.success(chargeItem);
    }

    @Cacheable(cacheNames = "AllCahrgeItems")
    public JsonResult findAllChargeItems() {
        List<ChargeItem> list = list();
        return ResultTool.success(list);
    }

    @CacheEvict(cacheNames = "AllCahrgeItems",allEntries = true)
    public JsonResult deleteChargeItem(String id) {
        boolean b = removeById(id);
        if (!b) {
            throw new ChargeItemExcception("删除失败");
        }
        return ResultTool.success("delete success");
    }

    @CacheEvict(cacheNames = "AllCahrgeItems",allEntries = true)
    public JsonResult updateChargeItem(ChargeItem chargeItem) {
        UpdateWrapper<ChargeItem> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", chargeItem.getId());
        wrapper.set("name", chargeItem.getName());
        wrapper.set("code", chargeItem.getCode());
        wrapper.set("community_id", chargeItem.getCommunityId());
        wrapper.set("community_name", chargeItem.getCommunityName());
        wrapper.set("money", chargeItem.getMoney());
        int update = chargeItemMapper.update(wrapper);
        if (update != 1) {
            throw new ChargeItemExcception("更新失败");
        }
        return ResultTool.success("update success");
    }
}
